<template>
  <div class="infinite-container">
    <template v-if="param.keyword != ''">
      <mu-list>
        <template v-for="(item,index) in searchData">
          <mu-list-item
            :title="item.type == 1 ? item.deptName : item.doctorName"
            @click="goDoctorDetail(item)"
            :key="index"
          >
            <span class="txtRight" slot="right">{{item.type == 1 ? "医生" : "科室"}}</span>
            <i class="arrowRight" slot="right"></i>
          </mu-list-item>
          <mu-divider :key="index" />
        </template>
      </mu-list>
    </template>
    <template v-else>
      <empty v-show="isEmptyDept"></empty>
      <div class="bgwhite">
        <mu-list>
          <template v-for="(item,index) in deptData">
            <mu-list-item
              :title="item.type==1?item.deptName:item.doctorName"
              @click="goDoctorDetail(item)"
              :key="index"
            >
              <span class="txtRight" slot="right">{{item.type == 1 ? "医生" : "科室"}}</span>
              <i class="arrowRight" slot="right"></i>
            </mu-list-item>
            <mu-divider :key="index" />
          </template>
        </mu-list>
      </div>
    </template>
  </div>
</template>

<script>
import empty from "@/components/empty";
import { commonAjaxJson, RESTFUL_API } from "../../api/api";
export default {
  data() {
    return {
      deptData: [],
      param: {
        keyword: ""
      },
      searchData: [],
      isEmpty: false,
      isEmptyDept: false
    };
  },
  components: {
    empty
  },
  mounted() {
    if (this.$route.query.keyword) {
      //搜索进来展示科室和医生列表
      this.param.keyword = this.$route.query.keyword;
      this.loadData();
    } else {
      //更多进来展示科室列表
      // this.deptData = this.$store.state.pageData.slice(0, this.$store.state.pageData.length - 1);
      let deptData = this.$store.state.pageData;
      this.deptData = deptData.filter(v => {
        return v.deptName || v.doctorName;
      });
      if (this.deptData.length == 0) {
        this.isEmptyDept = true;
      }
    }
  },
  methods: {
    loadData() {
      commonAjaxJson(RESTFUL_API.querySearch, this.param, true).then(res => {
        if (res.data.code == 200) {
          let d = res.data.data;
          if (d.doctorConfigExts.length == 0 && d.deptList.length == 0) {
            // 未搜索到结果
            this.isEmpty = true;
          } else {
            // 搜索到医生或者科室
            if (d.deptList.length > 0) {
              let depts = d.deptList;
              depts.forEach(item => {
                item.type = 1;
                item.deptName = item.consultDeptName;
              });
            }
            this.searchData = d.doctorConfigExts.concat(d.deptList);
          }
        } else {
          this.isEmpty = true;
        }
      });
    },
    goDoctorDetail(item) {
      let deptName = !!item.deptName ? item.deptName : "";
      if (item.type == 1) {
        this.$router.push(
          `/queryOnline/selDoctor?deptid=${item.deptId}&deptName=${deptName}`
        );
      } else {
        this.$router.push("/queryOnline/doctorDetail?docid=" + item.doctorId);
      }
    }
  }
};
</script>

<style scoped lang="less">
@import "../../common/css/component";
.txtRight {
  padding-right: 10px;
}
</style>
